<template>
  <div class="employees-container">
    <div class="app-container">
      <!-- 通用工具栏组件使用 -->
      <page-tools>
        <!-- 自定义左侧内容 -->
        <template #slot-left>
          <span>共19条记录</span>
        </template>

        <!-- 自定义右侧内容 -->
        <template #slot-right>
          <el-button type="danger" size="small" @click="$router.push('/excel')">导入excel</el-button>
          <el-button type="success" size="small">导出excel</el-button>
          <el-button type="primary" size="small" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="employeesList">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像" prop="staffPhoto" />
          <el-table-column label="手机号" prop="mobile" />
          <el-table-column label="工号" prop="workNumber" sortable :sort-method="sortMethodFn" />
          <el-table-column label="聘用形式" prop="formOfEmployment" :formatter="formatFormFn">
            <template v-slot="scope">
              <span>{{ formatFormOfEmplotmentFn(scope.row.formOfEmployment) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <el-table-column label="操作" width="280">
            <template>
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页区域 -->
          <el-pagination
            :current-page="query.page"
            :page-sizes="[10, 15, 20, 25]"
            :page-size="query.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <!-- 新增员工弹框组件 -->
      <el-dialog title="新增员工" :visible.sync="showDialog" @close="closeDialogFn">
        <emp-form
          ref="empForm"
          :show-dialog.sync="showDialog"
          :depart-list="departList"
          @enterEV="addFn"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getEmployeesListAPI, departmentsListAPI, addEmployeeAPI } from '@/api'
import Employees from '@/api/constant/employees'
import EmpForm from './components/empForm.vue'
// 树形结构的转换方法
import { transTree } from '@/utils'
export default {
  name: 'Employees',
  components: {
    EmpForm
  },
  data() {
    return {
      showDialog: false, // 添加员工组件的展示
      query: {
        page: 1, // 页码
        size: 10 // 每页条数
      },
      employeesList: [], // 员工列表
      departList: [], // 部门列表
      total: 0 // 数据总条数
    }
  },
  created() {
    this.getEmployeesListFn()
    this.getDepartmentsListFn()
  },
  methods: {
    async addFn(data) {
      await addEmployeeAPI(data)
      this.getEmployeesListFn()
    },
    // 请求部门列表的数据
    async getDepartmentsListFn() {
      const res = await departmentsListAPI()
      console.log(res)
      this.departList = transTree(res.data.depts, '')
    },
    // 获取员工列表
    async getEmployeesListFn() {
      // 调用获取员工列表的 API
      const res = await getEmployeesListAPI(this.query)
      console.log(res)
      // // 根据返回的状态码给用户提示
      // if (!res.success) return this.$message.error(res.message)
      // // 将返回的结果赋值 data 中的数据
      this.employeesList = res.data.rows
      this.total = res.data.total
    },
    // 每页显示的条数发生改变时触发
    handleSizeChange(newSize) {
      this.query.size = newSize
      this.getEmployeesListFn()
    },

    // 当前页面发生改变时触发
    handleCurrentChange(newPage) {
      this.query.page = newPage
      this.getEmployeesListFn()
    },
    formatFormOfEmplotmentFn(num) {
      const obj = {
        1: '正式',
        2: '非正式'
      }
      return obj[num]
    },
    // 格式化表格的某一项
    formatFormFn(row) {
      // 用数组的 find 方法找到 id = 1 的元素，再取出它的 value
      const obj = Employees.hireType.find((item) => Number(item.id) === Number(row.formOfEmployment))
      return obj ? obj.value : '未知'
    },
    sortMethodFn(a, b) {
      console.log(a, b)
      return a.workNumber - b.workNumber
    },
    // 关闭弹窗的事件方法
    closeDialogFn() {
      this.$refs.empForm.$refs.addForm.resetFields()
    },
    // 导入成功以后的回调函数
    handleSuccess({ results, header }) {
      console.log(results)
      console.log(header)
    }
  }
}
</script>

<style lang="scss" scoped></style>
